// pages/programmer/index/index.js
const api = require('../../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["发现", "我的", "筛选", "新增"],
    currentTab: 0,
    publicFangan: [],
    Domin: "https://zs.bocang.cc/",
    isLogin: wx.getStorageSync("isLogin"),
    curPage: 1,
    loadingHidden: false,
    loadingMoreHidden: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getFanganAllList();
    this.setData({
      isLogin: wx.getStorageSync("isLogin"),
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      isLogin: wx.getStorageSync("isLogin")
    });
  },

  navbarTap: function(e) {
    let that = this;
    if (e.currentTarget.id == 0) {
      that.getFanganAllList();
      that.getCurrentTab(e.currentTarget.offsetLeft, e.currentTarget.id)
    }
    if (e.currentTarget.id == 1) {
      if (that.data.isLogin) {
        that.getFanganList();
        that.getCurrentTab(e.currentTarget.offsetLeft, e.currentTarget.id)
      } else {
        that.getCurrentTab(0, 0)
        wx.navigateTo({
          url: '../../login/login/login',
        })
      }
    }
  },

  toMessageTap: function(e) {
    let that = this;
    var id = e.currentTarget.dataset.id;
    console.log("url", id);
    wx.navigateTo({
      url: '../../message/message-detail/message-detail?id=' + id,
    })
  },

  toPublicTap: function (e) {
    let that = this;
    var id = e.currentTarget.dataset.id;
    that.setPublicOrPrivate(id, 1);
  },

  toPrivateTap: function (e) {
    let that = this;
    var id = e.currentTarget.dataset.id;
    that.setPublicOrPrivate(id, 0);
  },

  setPublicOrPrivate: function (id, type) {
    let that = this;
    wx.showLoading();
    wx.request({
      url: api.PrivateFanan,
      data: {
        id: id,
        private: type
      },
      method: "POST",
      header: {
        'Content-Type': 'application/json',
        'X-bocang-Authorization': wx.getStorageSync('token')
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.error_code == 0) {
          wx.showToast({
            title: '设置成功',
            icon: "success"
          });
        }
        that.getFanganList();
      }
    })
  },

  toShareTap: function (e) {
    let that = this;
    var id = e.currentTarget.dataset.id;
    that.setData({
      shareId: id
    })
  },

  toDeleteTap: function(e) {
    let that = this;
    var id = e.currentTarget.dataset.id;
    wx.showLoading({
      "mask": true,
      title: "删除中..."
    })
    wx.request({
      url: api.DeleteFanan,
      data: {
        id: id
      },
      method: "POST",
      header: {
        'Content-Type': 'application/json',
        'X-bocang-Authorization': wx.getStorageSync('token')
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.error_code == 0) {
          wx.showToast({
            title: '删除成功',
            icon: "success"
          });
        }
        that.getFanganList();
      }
    })
  },

  getCurrentTab: function (offsetLeft, id) {
    let that = this;
    that.setData({
      sliderOffset: offsetLeft,
      currentTab: id
    });
  },

  getFanganAllList: function (loading) {
    let that = this;
    wx.showLoading({
      "mask": true,
      title: "加载中..."
    })
    wx.request({
      url: api.ProgrammerFanganAll,
      data: {
        page: that.data.curPage,
        per_page: 20,
        style: null,
        space: null
      },
      method: "POST",
      header: {
        'Content-Type': 'application/json',
        'X-bocang-Authorization': wx.getStorageSync('token')
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 404 || res.data.code == 700) {
          let newData = { loadingMoreHidden: false }
          if (!append) {
            newData.goods = []
          }
          that.setData(newData);
          return
        }
        if (res.data.error_code == 0) {
          let publicFangan = [];
          if (loading) {
            publicFangan = that.data.publicFangan
          }
          for (var i = 0; i < res.data.fangan.length; i++) {
            publicFangan.push(res.data.fangan[i]);
          }
          that.setData({
            loadingMoreHidden: true,
            publicFangan: publicFangan,
          });
        }
      },
    });
  },

  getFanganList: function () {
    let that = this;
    wx.showLoading({
      "mask": true,
      title: "加载中..."
    })
    wx.request({
      url: api.ProgrammerFangan,
      data: {
        page: 1,
        per_page: 20,
        style: null,
        space: null
      },
      method: "POST",
      header: {
        'Content-Type': 'application/json',
        'X-bocang-Authorization': wx.getStorageSync('token')
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.error_code == 0) {
          that.setData({
            publicFangan: res.data.fangan
          });
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      curPage: 1
    });
    if (this.data.currentTab == 0) {
      this.getFanganAllList();
    }
    if (this.data.currentTab == 1) {
      this.getFanganList();
    }
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.setData({
      curPage: this.data.curPage + 1
    });
    if (this.data.currentTab == 0) {
      this.getFanganAllList(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    // var id = this.data.shareId;
    // console.log("id", id);
    // return {
    //   title: "123",
    //   path: '/pages/message/message-detail/message-detail?id=' + id,
    //   success: function (res) {
    //     wx.showToast({
    //       title: '分享成功',
    //       icon: "success"
    //     })
    //   },
    //   fail: function (res) {
    //     wx.showToast({
    //       title: '分享失败',
    //       icon: "success"
    //     })
    //   }
    // }
  }
})